var mysql = require('mysql');
var config = require('./config.js');

var connection = mysql.createConnection({
    host: config.db.host,
    user: config.db.user,
    password: config.db.password,
    database: config.db.database,
    dateStrings: true
});
connection.connect(function(err){
    if(err){
        console.log(err);
    }else{
        console.log('连接数据库成功');
    }
});

module.exports.getAll = function (callback) {
    var sql = 'select * from employee ORDER BY id DESC';
    connection.query(sql, function (err, result) {
        if(err) {
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getAl = function (callback) {
    var sql = 'select * from admin_copy ORDER BY id DESC';
    connection.query(sql, function (err, result) {
        if(err) {
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getA = function (callback) {
    var sql = 'select * from admin ORDER BY id DESC';
    connection.query(sql, function (err, result) {
        if(err) {
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getByTi = function (filedName, value, callback) {
    var sql = 'select * from employee where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getByw = function (filedName, value, callback) {
    var sql = 'select * from employee where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getByField = function (filedName, value, callback) {
    var sql = 'select * from employee where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getByN = function (filedName, value, callback) {
    var sql = 'select * from admin where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getByF = function (filedName, value, callback) {
    var sql = 'select * from admin where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getBySou = function (filedName, value, callback) {
    var sql = 'select * from reply where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.getByX = function (filedName, value, callback) {
    var sql = 'select * from employee where ?? = ?';
    connection.query(sql, [filedName, value], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}
module.exports.insert = function(obj,callback){
    var name = obj.name;
    var gender = obj.gender;
    var birthdate = obj.birthdate;
    var department = obj.department;
    var remarks = obj.remarks;

    var sql = 'insert into employee(name, gender, birthdate, department, remarks)values(?, ?, ?, ?, ?);';
    connection.query(sql, [name, gender, birthdate, department, remarks],function(err, result){
        if(err) {
            console.log(err);
        }else{
            callback();
        }
    });
}
module.exports.plug = function(obj,callback){
    var username = obj.username;
    var sex = obj.sex;

    var sql = 'insert into admin_copy(username, sex)values(?, ?);';
    connection.query(sql, [username, sex],function(err, result){
        if(err) {
            console.log(err);
        }else{
            callback();
        }
    });
}
module.exports.charu = function(obj,callback){
    var username = obj.username;
    var password = obj.password;
    var numble = obj.numble;
    var mail = obj.mail;
    var sex = obj.sex;

    var sql = 'insert into admin(username, password,  sex, numble, mail)values(?, ?, ?, ?, ?);';
    connection.query(sql, [username, password, sex, numble, mail],function(err, result){
        if(err) {
            console.log(err);
        }else{
            callback();
        }
    });
}
module.exports.tianjia = function(obj,callback){
    var user = obj.user;
    var attach = obj.attach;
    var comments = obj.comments;
   

    var sql = 'insert into reply(user, attach, comments)values(?, ?, ?);';
    connection.query(sql, [user, attach, comments],function(err, result){
        if(err) {
            console.log(err);
        }else{
            callback();
        }
    });
}
module.exports.throwById = function (id, callback) {
    var sql = 'delete from admin_copy where id = ?';
    connection.query(sql, [id], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback();
        }
    });
}
module.exports.deleById = function (id, callback) {
    var sql = 'delete from employee where id = ?';
    connection.query(sql, [id], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback();
        }
    });
}
module.exports.updateById = function (obj, callback) {
    var sql = 'update employee set name = ?, gender = ?, birthdate = ?, department = ?, remarks = ? where id = ?';

    connection.query(sql, [obj.name, obj.gender, obj.birthdate, obj.department, obj.remarks, obj.id], function(err, result){
        if(err){
            console.log(err);
        }else{
            callback();
        }
    });
}

module.exports.checkAdmin = function (username, pwd, callback) {
    var sql = 'select * from admin where username = ? and password = ?';
    connection.query(sql, [username, pwd], function(err,result){
        if(err){
            console.log(err);
        }else{
            callback(result);
        }
    });
}